package game.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import game.level.pojo.ALevel;
import game.level.service.IALevelService;
import game.level.service.impl.ALevelServiceImpl;
import game.round.pojo.ARound;
import game.round.service.ARoundService;
import game.round.service.impl.ARoundServiceImpl;
import game.users.pojo.AUsers;
import game.users.service.IAUsersService;
import game.users.service.impl.AUsersServiceImpl;
import game.util.CaiquanUtil;

public class ClienMain
{
	/*所有数据库相关的信息*/
	private IAUsersService usersService=new AUsersServiceImpl();
	private IALevelService levelService = new ALevelServiceImpl();
	private ARoundService roundService = new ARoundServiceImpl();
	/*登录成功标识*/
	private AUsers loginUsers;
	public static void main(String[] args)
	{
		ClienMain clienMain = new ClienMain();
		clienMain.lanuch();
	}
	private void lanuch()
	{
		/*将后台连接到键盘上*/
		InputStream is = System.in;
		BufferedReader br = null ;
		try
		{
			br = new BufferedReader(new InputStreamReader(is, "utf-8"));
			/*存储用户输入的信息*/
			String line= "";
			/*程序启动的时候是无限死循环等待用户手动停止*/
			while(true)
			{
				/*菜单*/
				help();
				/*用户输入内容*/
				line = br.readLine();
				/*去掉用户输入的空白*/
				line = line.trim();
				if("1".equalsIgnoreCase(line))
				{
					/*登录*/
					this.loginUsers = this.login(br);
					if(this.loginUsers == null)
					{
						System.out.println("=登录失败=");
						break;
					}else
					{
						System.out.println("=登录成功=");
					}
				}else if("2".equalsIgnoreCase(line))
				{
					/*注册:向用户索要信息存储到数据库*/
					this.register(br);
				}else if("3".equalsIgnoreCase(line))
				{
					/*查询所有用户*/
					this.usersList(br);
				}else if("4".equalsIgnoreCase(line))
				{
					/*查询等级*/
					this.levelList(br);
				}else if("5".equalsIgnoreCase(line))
				{
					/*5:查询局*/
					this.roundList(br);
				}else if("6".equalsIgnoreCase(line))
				{
					/*开始新的一局*/
					this.roundInsert(br);
				}else if("7".equalsIgnoreCase(line))
				{
					if(this.logout(br))
					{
						break ;
					}
				}else
				{
					System.out.println("=输入有误:请核对后再重新输入=");
				}
			}
		}  catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	/*查询完了多少局*/
	private void roundList(BufferedReader br)
	{
		if(this.loginUsers == null)
		{
			System.out.println("=请先登录=");
			return ;
		}
		System.out.println(this.loginUsers.getNickName()+"=游戏的历史记录=");
		System.out.println("id\t玩家id\t共多少局\t胜利了多少局\t输了多少局\t状态\t创建时间\t更新时间\t发布时间");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/*ID 调用用户输入的di*/
		condMap.put("usersId", this.loginUsers.getId());
		List<ARound> roundList = this.roundService.findCondListRondService(condMap);
		for (Iterator iterator = roundList.iterator(); iterator.hasNext();)
		{
			ARound round = (ARound) iterator.next();
			/*结果对应用户表*/
			AUsers users = round.getUsers();
			/*"id,玩家id,用户名,共多少局,胜利了多少局,输了多少局,状态,创建时间,更新时间,发布时间"*/
			System.out.println(round.getId()+"\t"+users.getNickName()+"\t"+
						round.getTotalCount()+"\t"+round.getWinCount()+"\t"+round.getLoseCount()+"\t"
						+round.getStatus()+"\t"+round.getCreateTime().toLocaleString()+"\t"+round.getUpdateTime().toLocaleString()+"\t"+round.getPubTime().toLocaleString());
		}
	}
	/*查询所有用户
	 * 查询自己的等级*/
	private void usersList(BufferedReader br)
	{
		 if(this.loginUsers == null)
		 {
			 System.out.println("=请先登录=");
			 return ;
		 }
		 System.out.println("id\t等级\t邮箱\t昵称\t比分\t状态\t创建时间\t更新时间\t发布时间");
		 /* 查询的条件
		  *	按照发布时间的条件
		  * */
		 Map<String, Object> condMap = new HashMap<String, Object>();
		 condMap.put("id", this.loginUsers.getId());
		 List<AUsers> usersList = this.usersService.findCondListService(condMap);
		 for (Iterator iterator = usersList.iterator(); iterator.hasNext();)
		{
			AUsers users = (AUsers) iterator.next();
			/*获取 关联对象*/
			ALevel level = users.getLevel();
								/*"id\t等级\t邮箱\t昵称\t比分\t状态\t创建时间\t更新时间\t发布时间*/
			System.out.println(users.getId()+"\t"+level.getName()+"\t"+users.getEmail()+"\t"+users.getNickName()+"\t"+users.getScore()+"\t"+users.getStatus()+"\t"+users.getCreateTime().toLocaleString()+"\t"
								+users.getUpdateTime().toLocaleString()+"\t"+users.getPubTime().toLocaleString());
		}
	}
	/* 
	 * 	退出界面 */
	private boolean logout(BufferedReader br)
	{
		try
		{
			System.out.println("=确定要退出吗?(y/n)=");
			String line = br.readLine();
			if("y".equalsIgnoreCase(line))
			{
				System.out.println("=谢谢惠顾,不服在来战=");
				/*登录的账号清空*/
				this.loginUsers = null;
				return true;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return false;
	}
	/*开始新的一局
	 * 告诉用户怎么玩
	 * 要做好记录并及时保存数据
	 * */
	private void roundInsert(BufferedReader br)
	{
		if(this.loginUsers == null)
		{
			System.out.println("=请先登录=");
			return;
		}
		CaiquanUtil caiquanUtil = new CaiquanUtil();
		Map<String, Integer> resultMap = caiquanUtil.wan(br);
		System.out.println("=resultMap="+resultMap);
		/*保存数据库*/
		int totalCount = resultMap.get("totalCount");
		int winCount = resultMap.get("winCount");
		int pingCount = resultMap.get("pingCount");
		int loseCount = resultMap.get("loseCount");
		ARound round = new ARound();
		/*用户的id也需要*/
		round.setUsersId(this.loginUsers.getId());
		round.setTotalCount(totalCount);
		round.setWinCount(winCount);
		round.setLoseCount(loseCount);
		/*赢一把两分*/
		round.setPerScore(2);
		round.setStatus(Byte.valueOf("1"));
		round.setCreateTime(new Date());
		round.setUpdateTime(new Date());
		round.setPubTime(new Date());
		int res = this.roundService.insertRondService(round);
		if(res > 0)
		{
			System.out.println("=已经保存成功(round)=");
		}
	}
	/*查询等级
	 * 从等级表中查询 
	 * */
	private void levelList(BufferedReader br)
	{
		if(this.loginUsers == null)
		{
			System.out.println("=请先登录=");
			return ;
		}
		System.out.println("id\t级别\t开始分数\t状态\t创建时间\t更新时间\t发布时间");
		/*
		 * 查询的条件
		 * 按照发布时间查询
		 * */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("status", "1");
		List<ALevel> levelList = this.levelService.findOneListLevelService(condMap);
		for (Iterator iterator = levelList.iterator(); iterator.hasNext();)
		{
			ALevel level = (ALevel) iterator.next();
			System.out.println(level.getId()+"\t"+level.getName()+"\t"+level.getStScore()+"\t"+
					level.getStatus()+"\t"+level.getCreateTime().toLocaleString()+"\t"+
					level.getUpdateTime().toLocaleString()+"\t"+level.getPubTime().toLocaleString());
		}
	}
	/*注册账户
	 * 将用户的信息记录到数据库*/
	private void register(BufferedReader br)
	{
		System.out.println("=请填写相关的信息=");
		System.out.println("=格式:邮箱;密码;昵称=");
		System.out.println("如:123@qq.com;1234;战起来");
		try
		{
			String line = br.readLine();
			/*去掉用户输入的空格*/
			line = line.trim();
			String [] lines = line.split(";");
			if(lines.length == 3)
			{
				AUsers users = new AUsers();
				/*将拆分后的额字符串 赋值给用户*/
				users.setEmail(lines[0]);
				users.setPassword(lines[1]);
				users.setNickName(lines[2]);
				/*状态一定要启动*/
				users.setStatus(Byte.valueOf("1"));
				users.setCreateTime(new Date());
				users.setUpdateTime(new Date());
				users.setPubTime(new Date());
				int res = this.usersService.insertService(users);
				if(res>0)
				{
					System.out.println("=注册成功:请登录=");
				}
			}else
			{
				System.out.println("=填写信息有误:请按照规定填写=");
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	/*登录
	 * 判断用户输入是否正确
	 * 账户状态是否禁用*/
	private AUsers login(BufferedReader br)
	{
		try
		{
			Map<String, Object> condMap = new HashMap<String, Object>();
			System.out.println("=请输入邮箱:=");
			String line = br.readLine();
			/*用邮箱去查询用户是否存在*/
			condMap.put("email", line);
			AUsers users = this.usersService.findOndAUsersService(condMap);
			if(users !=null)
			{
				System.out.println("=请输入密码=");
				line = br.readLine();
				if(users.getPassword().equalsIgnoreCase(line))
				{
					/*输入的密码正确*/
					if(users.getStatus() == 1)
					{
						/*登录成功*/
						return users;
					}else
					{
						/*该用户被禁用*/
						System.out.println("=当前用户已被禁用=");
					}
				}else
				{
					System.out.println("=输入的密码不正确,请核对后再从新输入=");
				}
			}else
			{
				System.out.println("=输入的邮箱格式不正确,请核对后再重新输入=");
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}
	/*登录的属性菜单*/
	private void help()
	{
		System.out.println("1:登录");
		System.out.println("2:注册用户");
		System.out.println("3:查询所有用户");
		System.out.println("4:查询所有等级");
		System.out.println("5:查询局");
		System.out.println("6:新开一局");
		System.out.println("7:退出");
	}
}
